package com.ymatou.iapi.topicrecbizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.topicrecbizer.parameter.NumberBean;
import com.ymatou.iapi.topicrecbizer.service.NumberCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.common.entitie.Total;

public class Ts_Number {
	private static NumberBean numberBean;
	private static NumberCall numberCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新首页商品数/围观数接口");
	}

	@Before
	public void caseUp() {
		numberBean = new NumberBean();
		numberCall = new NumberCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(numberCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Number_001() {
		Logger.start(true,"新首页商品数/围观数接口-检查接口返回公共信息 ");
		try {
			//设置参数
			
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
	
			
			numberBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			numberBean.setQuery("term(source,0)");
			numberBean.setLimit("0,0");
			
			numberCall.setData(numberBean);
			numberCall.callService();
			
			Logger.verifyEquals("OK",numberCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(numberCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, numberCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, numberCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(numberCall.getCommonInfo("hostip"), "验证是否有返回服务器ip");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Number_002() {
		Logger.start(true,"新首页商品数/围观数接口-扫货商品-检查接口返回总商品数是否正确 ");
		try {
			//设置参数
			String type = "0";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			numberBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			numberBean.setQuery("term(source,"+type+")");
			numberBean.setLimit("0,0");
			
			numberCall.setData(numberBean);
			numberCall.callService();
			
			int total = numberCall.getTotal();
			JSONObject searchInfo =  (JSONObject) numberCall.getSearchInfo();
			String num =  (String) searchInfo.get("productCount");
			
			Logger.verifyEquals(total, Integer.parseInt(num),"检查接口返回总记录数是否正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Number_003() {
		Logger.start(true,"新首页商品数/围观数接口-现货商品-检查接口返回总商品数是否正确 ");
		try {
			//设置参数
			String type = "1";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			numberBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			numberBean.setQuery("term(source,"+type+")");
			numberBean.setLimit("0,0");
			
			numberCall.setData(numberBean);
			numberCall.callService();
			
			int total = numberCall.getTotal();
			JSONObject searchInfo =  (JSONObject) numberCall.getSearchInfo();
			String num =  (String) searchInfo.get("productCount");
			
			Logger.verifyEquals(total, Integer.parseInt(num),"检查接口返回总记录数是否正确");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Number_004() {
		Logger.start(true,"新首页商品数/围观数接口-扫货商品-检查接口返回是否包含商品数和围观数 ");
		try {
			//设置参数
			String type = "0";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			numberBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+"");
			numberBean.setQuery("term(source,"+type+")");
			numberBean.setLimit("0,0");
			
			numberCall.setData(numberBean);
			numberCall.callService();
			
			int total = numberCall.getTotal();
			JSONObject searchInfo =  (JSONObject) numberCall.getSearchInfo();
			Logger.verifyEquals(true, searchInfo.has("productCount"), "检查接口是否有返回商品数");
			Logger.verifyEquals(true, searchInfo.has("live_uv"), "检查接口是否有返回围观数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
